import importlib
import json

from .mouse import run_mouse_operation
from .check_point import run_check_point
from .browser import run_browser_operation
from .multiple import run_multiple_operation
from .keyboard import run_keyboard_operation
from .tool import run_tool_operation
from .select import run_select_operation
from utils.selenium.perform import WPerform


class OptionType:
    send_keys = 1
    clear = 2
    mouse = 3
    checkpoint = 4
    browser = 5
    tool = 6
    select = 7
    template = 100
    custom_operation = 101


def run_use_case(perform: WPerform, use_case: dict, **kwargs):
    """执行用例"""
    # 是否打开新页面
    start_page_path = use_case.get('start_page_path')
    if perform.driver:
        open_new_page = use_case.get('open_new_page', False)
        if open_new_page:
            if open_new_page:
                perform.open_new_page(start_page_path)
                perform.sleep(1)
                perform.switch_window()
            else:
                perform.update_page_url(start_page_path)
    else:
        perform.start_page_path = start_page_path
        perform.create_driver()
        perform.start()

    # 需要执行的操作列表
    run_operation_list: dict = use_case.get('run_operation_list')
    for operation_item in run_operation_list:
        # 前置睡眠时间
        perform.sleep(operation_item.get('start_time_interval'))
        # 操作类型
        option_type = operation_item.get('option_type')
        if option_type == OptionType.template:
            run_template(perform, operation_item)
        elif option_type == OptionType.custom_operation:
            run_custom_operation(perform, operation_item)
        else:
            run_operation(perform, operation_item, {
                'run_use_case': run_use_case,
                'perform': perform,
            })
        # 后置睡眠时间
        perform.sleep(operation_item.get('end_time_interval'))


def run_template(perform: WPerform, operation_item: dict):
    """执行模板"""
    templates = operation_item.get('templates')
    for template in templates:
        perform.send_msg('开始执行模板【%s】' % template.get('name'))
        # 执行模板
        run_use_case(perform, template)
        perform.send_msg('模板【%s】执行完成' % template.get('name'))


def run_custom_operation(perform: WPerform, operation_item: dict):
    """执行自定义操作"""
    custom_operation = operation_item.get('custom_operation')
    custom_operation_name = custom_operation.get('name')
    perform.send_msg('自定义操作【%s】开始' % custom_operation_name)
    # 脚本路径
    script_path = 'custom_operations.%s' % custom_operation.get('script_path')
    metaclass = importlib.import_module(script_path)
    # 获取参数
    custom_operation_data = operation_item.get('custom_operation_data')
    query = json.loads(custom_operation_data) if custom_operation_data else None
    # 执行方法
    metaclass.run(perform, query)
    perform.send_msg('自定义操作【%s】结束' % custom_operation_name)


def run_operation(perform: WPerform, item, other_query):
    """执行普通操作"""
    perform.send_msg('操作【%s】开始' % item.get('name'))
    option_type = item.get('option_type')
    if option_type == OptionType.send_keys:  # 键盘操作
        run_keyboard_operation(perform, item, other_query)
    elif option_type == OptionType.mouse:  # 鼠标
        run_mouse_operation(perform, item, other_query)
    elif option_type == OptionType.clear:  # 清除
        run_multiple_operation(perform, item, 'clear', (), other_query)
    elif option_type == OptionType.checkpoint:  # 设置检查点
        run_check_point(perform, item, other_query)
    elif option_type == OptionType.browser:  # 浏览器操作
        run_browser_operation(perform, item)
    elif option_type == OptionType.tool:  # 工具
        run_tool_operation(perform, item, other_query)
    elif option_type == OptionType.select:  # 选择器
        run_select_operation(perform, item, other_query)
    perform.send_msg('操作【%s】结束' % item.get('name'))



